# coding:utf-8
# Import the common package
import os
import unittest
from appium import webdriver
from time import sleep
from airtest.core.api import *
from senseface_app_test import AdbConnect

# 设置路径信息
PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)


class LoginAndroidTests(unittest.TestCase):

    def setUp(self):
        # 初始化测试平台
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '9'  # Android版本
        # desired_caps['deviceName'] = '127.0.0.1:62001'　　  # 连接到的设备名称，这个是夜神模拟器的名称
        desired_caps['deviceName'] = 'HUAWEI Mate10'  # 设备名称 3HX5T17415004741
        # desired_caps['app'] = 'D:\apk\爱壁纸.apk'　　#app的路径
        # desired_caps['app'] = "D:\wjj\app_package\3.4.1_341283_201905050958-release.apk" #app的路径
        desired_caps['appPackage'] = 'com.sensetime.iva.senseface'  # 包名
        desired_caps['appActivity'] = 'com.sensetime.iva.ui.login.LoginActivity' #APP登陆页的Activity
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

    def tearDown(self):
        self.driver.quit()

    def test(self):

        try:

            # 判断手机是否连接电脑
            # device = os.popen("adb get-serialno").readline()
            # if len(device) != 0:
            #     return device
            # else:
            #     print("手机连接有问题，请重新连接.")

            # 测试登录页
            print("start test...")

            # 判断是否安装APP
            wallpaper = self.driver.is_app_installed("com.sensetime.iva.senseface")
            if wallpaper:

                # self.driver.remove_app("com.sensetime.iva.senseface")
                #启动APP
                self.driver.launch_app()

                sleep(8)
                #点击用户名输入框,并输入账号
                self.driver.find_element_by_id('com.sensetime.iva.senseface:id/input_login_username').clear().send_keys("wjj1")
                # driver.find_elements_by_class_name("android.widget.EditText")[1].clear().send_keys("wjj1")

                sleep(4)
                # 点击密码输入框并输入密码
                self.driver.find_element_by_id('com.sensetime.iva.senseface:id/input_login_pwd').clear().send_keys("wjj123456")
                # driver.find_elements_by_class_name("android.widget.EditText")[2].clear().send_keys("wjj123456")
                sleep(5)
                self.driver.swipe(1305, 1433, 0, 0, 3)
                sleep(5)
                # self.driver.keyevent(4)  # 返回键
                # self.driver.press_keycode(4)  # 返回键


                # 点击IP输入框
                # self.driver.find_element("请输入服务器地址").click()
                # self.driver.find_element_by_id("com.sensetime.iva.senseface:id/input_login_ip").click()
                self.driver.swipe(470, 1537, 0, 0, 3)
                sleep(5)
                #隐藏键盘
                # self.driver.hide_keyboard()
                #在IP输入框中输入ip各字段
                self.driver.find_element_by_id("com.sensetime.iva.senseface:id/ip_1").send_keys("10")
                sleep(5)
                self.driver.find_element_by_id("com.sensetime.iva.senseface:id/ip_2").click()
                self.driver.find_element_by_id("com.sensetime.iva.senseface:id/ip_2").send_keys("111")
                sleep(5)
                self.driver.find_element_by_id("com.sensetime.iva.senseface:id/ip_3").send_keys("32")
                sleep(5)
                self.driver.find_element_by_id("com.sensetime.iva.senseface:id/ip_4").send_keys("70")
                sleep(5)
                #点击“确定”
                self.driver.find_element_by_id("com.sensetime.iva.senseface:id/tv_sure").click()
                sleep(5)
                # 点击“登录”
                self.driver.find_element_by_id("com.sensetime.iva.senseface:id/btn_login").click()
                sleep(5)
                touch()
                touch(Template(r"./tp_01.png", record_pos=(0.366, 0.201), resolution=(1440, 2560)))


            else:
                self.driver.install_app("D:\\python\\3.4.1_341283_201905050958-release.apk")
                sleep(30)
        except Exception as e:
            print("e"+e)
        finally:
            sleep(3)
            self.driver.press_keycode(3)


if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(LoginAndroidTests)
    unittest.TextTestRunner(verbosity=2).run(suite)